/**
 * @version 1.0
 * @author GaoTong
 * @description 开启服务器端socket服务线程。用于处理所有用户的连接。
 * 				每连接一个用户，就另外创建一个新的线程处理新的连接！
 * @modify 2012/12/20
 */
package flex01;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;

import modle.Player;
import modle.Table;
import servlet.StartSocketServlet;

/*
 * 开启服务线程。只会有一个，用于处理用户的连接
 */
public class SocketServer implements Runnable {
	private int port;
	public static boolean status = true;
	public ServerSocket server = null;
	
	public char type;
	
	public static final String NEW_PLAYER = "newplayer";
	
	public static final String ADDTABLE = "addTable";
	public static final String GAMEIN1 = "gameIn1";
	public static final String GAMEIN2 = "gameIn2";
	public static final String GAMEOUT1 = "gameOut1";
	public static final String GAMEOUT2 = "gameOut2";
	public static final String TABLECHG = "tableChg";
	public static final String PLAYEREXIT = "playerExit";
	public static final String REFRESH = "refresh";
	public static final String GAMEOUT = "gameOut";
	public static final String ROOMCHAT = "roomChat";
	public static final String ROOMOUT = "roomout";
	//记录 所有玩家列表，只要在房间里的都存在这里
	public ArrayList<Player> clientList = new ArrayList<Player>();
	
	//记录有玩家的桌子s
//	public ArrayList<Table> tableList = new ArrayList<Table>();
	public Hashtable<Integer,Table> tableMap = new Hashtable<Integer,Table>();
	//private SocketThread socketThread = new SocketThread();
	
	
	public SocketServer(int port){
		this.port = port;
	}
	
	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public SocketServer(){
		
		//type = StartSocketServlet.socketServerList.indexOf(this);
	}
	
	public void run() {
		//创建Socket服务器
		server = SocketUtil.getServerSocket(port);
		//
		while(status){
			
			//等待得到用户的连接
			Socket socket = SocketUtil.getSocket(server);
			
			//把当前连接的用户，加入到总用户列表
			
			System.out.println(server.getLocalPort()+"连接了+人数："+ clientList.size());
			System.out.println(socket.hashCode());
			
			
			
			//开启新的处理客户端线程！
			new Thread(new SocketThread(socket,"UTF-8",this)).start();
		}
	}
	
	/**
	 * 启动服务器
	 */
	public void startSocket(){
		new Thread(this).start();
	}
	
	/**
	 * 关闭服务器
	 */
	public void stopSocket(){
		
		status = false;
		if(server!=null && !server.isClosed()){
			try {
				server.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 给服务器发送消息
	 * @param msg
	 */
	public void sendMessage2All(String msg){
		System.out.println("test");
		System.out.println(clientList.size());
		for(Player player:clientList){
			sendToClient(player.getSocket(),msg);
		}
	}
	
	public void sendToClient(Socket socket,String msg){
		try {
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
					socket.getOutputStream(), "utf-8"));
			bw.append(msg);
			bw.flush();
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void sendMessage2AllRooms(String msg){
		for(SocketServer server:StartSocketServlet.socketServerList)
			server.sendMessage2All(msg);
	}
	
	public void send2AllNoMyRoom(String msg){
		for(SocketServer server:StartSocketServlet.socketServerList)
			if(server != this)
				server.sendMessage2All(msg);
	}
	
	
	
	public void send2noSomeone(Player player,String msg){
		for(Player p:clientList){
			if(p != player)
				sendToClient(player.getSocket(),msg);
		}
	}
	
	
	public void send2Table(Table table, String msg){
		for(Player p:table.players)
			sendToClient(p.getSocket(), msg);
	}
	
}
